//
// Unihan Input Method
//
// Copyright © 2002-2007 Sun Wah Linux Ltd.
//
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
// USA
//
// $Id: WordNew_cb.c 757 2007-05-10 08:19:31Z roger $
//

#include <string.h>
#include <gtk/gtk.h>
#include <glade/glade.h>
#include <gconf/gconf-client.h>

#include "WordNew_cb.h"
#include "prefedit.h"
#include "list_ctrl.h"
#include "list_factory.h"
#include "dialog.h"

void
on_Word_new_Tree_View_Selection_changed(GtkTreeSelection *selection,
		gpointer data)
{
	return;
}

void
on_Word_new_Button_OK_clicked(GtkButton *button, gpointer data)
{
	GtkWidget *w;
	GConfClient *client = gconf_client_get_default();

	if (!word_list_new) {
		w = glade_xml_get_widget(xml_WordNew, "windowWordNew");
		gtk_widget_destroy(w);
		return;
	}
	gconf_client_set_int(client,
			PREFEDIT_COMMAND_KEY,
			REFRESH_WORD_LIST,
			NULL);

	w = glade_xml_get_widget(xml_WordNew, "windowWordNew");
	g_object_unref(G_OBJECT(xml_WordNew));
	gtk_widget_destroy(w);
}

void
on_Word_new_Button_Cancel_clicked(GtkButton *button, gpointer data)
{
	GtkWidget *w;

	w = glade_xml_get_widget(xml_WordNew, "windowWordNew");
	g_object_unref(G_OBJECT(xml_WordNew));
	gtk_widget_destroy(w);
}

void
on_Word_new_Button_Add_clicked(GtkButton *button, gpointer data)
{
	GtkWidget *w;
	const gchar *key = NULL;
	const gchar *word = NULL;
	WordDefGroup *pWDG = NULL;
	GList *l = word_list;

	w = glade_xml_get_widget(xml_WordNew, "entryKey");
	key = gtk_entry_get_text(GTK_ENTRY(w));
	if (!key) {
		warning_dialog(_("Please input key!"));
		return;
	}

	while (l) {
		pWDG = (WordDefGroup *)l->data;
		if (strcmp(pWDG->key, key) == 0) {
			warning_dialog(_("You had defined this key, please redefine!"));
			return;
		}
		l = g_list_next(l);
	}

	l = word_list_new;
	while (l) {
		pWDG = (WordDefGroup *)l->data;
		if (strcmp(pWDG->key, key) == 0) {
			warning_dialog(_("You hand defined this key, please redefine!"));
			return;
		}
		l = g_list_next(l);
	}

	w = glade_xml_get_widget(xml_WordNew, "entryWord");
	word = gtk_entry_get_text(GTK_ENTRY(w));
	if (!word) {
		warning_dialog(_("Please input word!"));
		return;
	}

	pWDG = g_new0(WordDefGroup, 1);
	pWDG->key = g_strdup(key);
	pWDG->word = g_strdup(word);
	word_list_new = g_list_append(word_list_new, pWDG);

	w = glade_xml_get_widget(xml_WordNew, "treeviewWordNew");
	Word_new_refresh_treeview_by_word_list(GTK_TREE_VIEW(w), word_list_new);

	w = glade_xml_get_widget(xml_WordNew, "entryKey");
	gtk_entry_set_text(GTK_ENTRY(w), "");

	w = glade_xml_get_widget(xml_WordNew, "entryWord");
	gtk_entry_set_text(GTK_ENTRY(w), "");
}

void
on_Word_new_Button_Del_clicked(GtkButton *button, gpointer data)
{
	GtkWidget *treeview = NULL;
	GList *l = word_list_new;
	GtkTreeSelection *selection = NULL;
	GtkTreeModel *model = NULL;
	GtkTreeIter iter;
	gchar *key;
	WordDefGroup *pWDG = NULL;

	treeview = glade_xml_get_widget(xml_WordNew, "treeviewWordNew");
	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));

	if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
		gtk_tree_model_get(model, &iter,
				STORE_1, &key,
				-1);
	}
	else {
		warning_dialog(_("Please choose Word you want to delete!"));
		return;
	}
	
	while (l) {
		pWDG = (WordDefGroup *)l->data;
		if (strcmp(pWDG->key, key) == 0)
			break;
		l = g_list_next(l);
	}

	if (l->next)
		l->next->prev = l->prev;
	if (l->prev)
		l->prev->next = l->next;
	else
		word_list_new = l->next;
	if (word_list_new)
		word_list_new->prev = NULL;
	l->next = NULL;
	l->prev = NULL;
	g_free(pWDG->key);
	g_free(pWDG->word);
	g_list_free(l);
	Word_new_refresh_treeview_by_word_list(GTK_TREE_VIEW(treeview),
			word_list_new);
}

void
on_Word_new_Window_destroy(GtkWidget *widget, gpointer data)
{
	g_object_unref(G_OBJECT(xml_WordNew));
	gtk_widget_destroy(widget);
}

// vi:ts=4:nowrap:cin:expandtab
